require("IntercectionWithEntitiesInteractor")

IntercectionWithEntitiesInteractorManager = class("IntercectionWithEntitiesInteractorManager")

function IntercectionWithEntitiesInteractorManager:initialize(entities, propertyName)

	self.entities = entities
	self.propertyName = propertyName
	self.lastRefreshBeat = -1
	self.intercection = false
	
	for i, entity in ipairs(entities)
	do
		entity:addInteractor(propertyName, IntercectionWithEntitiesInteractor:new(self))
	end
end

---

function IntercectionWithEntitiesInteractorManager:getIntercection()

	if gField.beat ~= self.lastRefreshBeat
	then
		local intercection = {}
	
		for i, entity in ipairs(self.entities)
		do
			if i == 1
			then
				
				-- first entity initialise the intercection
				
				for value, pobability in pairs(entity.properties[self.propertyName])
				do
					intercection[value] = true
				end
			else
			
				-- reduce intercection
				
				for intercectionValue, intercectionFlag in pairs(intercection)
				do
					if not entity.properties[self.propertyName][intercectionValue]
					then
						intercection[intercectionValue] = nil
					end
				end				
			end
		end
		
		self.intercection = intercection
		self.lastRefreshBeat = gField.beat
	end
	
	return self.intercection
end